package com.bootlever.framework.redis.configuration;

import com.bootlever.framework.common.utils.ReflectUtils;
import com.bootlever.framework.redis.cfg.BootLeverJedisPoolConfig;
import com.bootlever.framework.redis.cfg.BootLeverRedissonConfig;
import com.bootlever.framework.redis.constant.EnumRedisType;
import com.bootlever.framework.redis.properties.RedisProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.InvocationTargetException;

@Configuration
public class RedissonConfiguration {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    @ConditionalOnExpression("'${bootlever.framework.redis.redisson.autoconfiguration:true}'=='true'")
    @ConditionalOnMissingBean(RedissonClient.class)
    public RedissonClient redissonClient() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        BootLeverRedissonConfig config = redisProperties.getRedisson();
        String type = redisProperties.getType();
        if (EnumRedisType.SINGLETON.getType().equals(type)) {
            SingleServerConfig singleServerConfig = config.useSingleServer();
            ReflectUtils.copySameProperties(config.getSingleton(),singleServerConfig);//配置类的属性复制过去
            ReflectUtils.copySameProperties(redisProperties,singleServerConfig);//公共类的属性复制过去
            singleServerConfig.setAddress("redis://" + redisProperties.getAddress());
            RedissonClient redissonClient = Redisson.create(config);
            return redissonClient;
        } else if (EnumRedisType.SENTINEL.getType().equals(type)) {
            SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
            ReflectUtils.copySameProperties(config.getSentinel(),sentinelServersConfig);
            ReflectUtils.copySameProperties(redisProperties, sentinelServersConfig);
            for (String addr : redisProperties.getNodes().split(",")){
                sentinelServersConfig.addSentinelAddress(addr);
            }
            RedissonClient redissonClient = Redisson.create(config);
            return redissonClient;
        } else if (EnumRedisType.CLUSTER.getType().equals(type)) {
            ClusterServersConfig clusterServersConfig = config.useClusterServers();
            ReflectUtils.copySameProperties(config.getCluster(), clusterServersConfig);
            ReflectUtils.copySameProperties(redisProperties, clusterServersConfig);
            for (String addr : redisProperties.getNodes().split(",")){
                clusterServersConfig.addNodeAddress(addr);
            }
            RedissonClient redissonClient = Redisson.create(config);
            return redissonClient;
        }
        return null;
    }


}
